//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4x4 g_mWorldViewProjection : WORLDVIEWPROJ;	// World * View * Projection matrix
float4x4 g_mView : VIEW;							// View matrix
float4x4 g_mWorld : WORLD;							// World matrix
float g_fZNear : ZNEAR;								// Camera z near distance
float g_fZFar : ZFAR;								// Camera z far distance

//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
	float4 Position   : POSITION;   // vertex position 
	float4 Diffuse    : COLOR0;     // vertex diffuse color (note that COLOR0 is clamped from 0..1)
};


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION )
{
	VS_OUTPUT Output;

	Output.Position = mul( vPos, g_mWorldViewProjection );

	// set color based on camera distance
	float3 ObjPos = mul( vPos, g_mWorld );
	float3 CamPos = g_mView[3].xyz;
	float Color = 1.0 - ( ( length( ObjPos - CamPos ) - g_fZNear ) / ( g_fZFar - g_fZNear ) );
	Output.Diffuse = float4( Color, Color, Color, 1.0 );
	//Output.Diffuse = float4( 1.0, 1.0, 1.0, 1.0 );

	return Output;    
}


//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
	float4 RGBColor : COLOR0;  // Pixel color    
};


//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
//       color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In ) 
{ 
	PS_OUTPUT Output;

	Output.RGBColor = In.Diffuse;

	return Output;
}


//--------------------------------------------------------------------------------------
// Renders scene to render target
//--------------------------------------------------------------------------------------

technique RenderScene
{
    pass P0
    {          
        VertexShader = compile vs_2_0 RenderSceneVS();
        PixelShader  = compile ps_2_0 RenderScenePS();
    }
}
